//
//  OMOrderEvaluateSubmitViewController.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/17.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMOrderEvaluateSubmitViewController.h"
#import "OMOrderEvaluateSelectTableCell.h"
#import "OMOrderEvaluateStarTableCell.h"
#import "OMOrderEvaluateTagTableCell.h"
#import "OMInputTableCell.h"

@interface OMOrderEvaluateSubmitViewController ()
@property (nonatomic, strong)OMOrderEvaluateSelectTableCell *selectCell;
@property (nonatomic, strong)OMOrderEvaluateStarTableCell *shopCell;
@property (nonatomic, strong)OMOrderEvaluateStarTableCell *shipCell;
@property (nonatomic, strong)OMOrderEvaluateStarTableCell *menCell;
@property (nonatomic, strong)OMInputTableCell *inputCell;

@property (nonatomic, strong) OMOrderEvaluateTagTableCell *tagCell;
@property (nonatomic, strong) OMInputTableCell *moreCell;
@end

@implementation OMOrderEvaluateSubmitViewController {
    NSArray *_starArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
}

- (void)setupUI
{
    self.title = @"发表评价";
    [self.confirmButton setTitle:@"提交评价" forState:UIControlStateNormal];
    _selectCell = [OMOrderEvaluateSelectTableCell new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SIZE(20))];
    self.tableView.sectionFooterHeight = DBL_EPSILON;
    self.tableView.sectionHeaderHeight = SM_SIZE(20);

    _shopCell = [OMOrderEvaluateStarTableCell new];
    _shopCell.elementLabel.text = @"商家服务";
    _shipCell = [OMOrderEvaluateStarTableCell new];
    _shipCell.elementLabel.text = @"配送服务";
    _menCell = [OMOrderEvaluateStarTableCell new];
    _menCell.elementLabel.text = @"人员服务";
    _starArray = @[_shopCell,_shipCell,_menCell];


    _inputCell = [OMInputTableCell new];
    _inputCell.maxLength = 200;
    _inputCell.placehold = @"点击可输入更多补充意见";


    _tagCell = [OMOrderEvaluateTagTableCell new];
    _moreCell = [OMInputTableCell new];
    _moreCell.maxLength = 200;
    _moreCell.placehold = @"没找到合适的标签？点这里发表意见吧~";

    @weakify(self);
    [[RACSignal combineLatest:@[RACObserve(self.selectCell, type)] reduce:^id{
        @strongify(self);
        return [self check];
    }] subscribeNext:^(id x) {
        @strongify(self);
        [self.tableView reloadData];
        self.confirmButton.enabled = [x boolValue];
    }];

    [[RACSignal combineLatest:@[
                                RACObserve(_shopCell.starView,point),
                                RACObserve(_shipCell.starView,point),
                                RACObserve(_menCell.starView,point),
                                self.inputCell.textView.rac_textSignal,
                                RACObserve(self.tagCell,selectedMessage),
                                self.moreCell.textView.rac_textSignal]
                       reduce:^id{
                           @strongify(self);
                           return [self check];
                       }] subscribeNext:^(id x) {
                           @strongify(self);
                           self.confirmButton.enabled = [x boolValue];
                       }];
}


- (NSNumber*)check
{
    if (self.selectCell.type == EOEvaluateTypeGood) {
        if (!self.shopCell.starView.point||!self.shipCell.starView.point||!self.menCell.starView.point) {
            return @(NO);
        }
        /*else if (!self.inputCell.textView.text.length) {
         return @(NO);
         }*/
    }else {
        if (!self.tagCell.selectedMessage.length && !self.moreCell.textView.text.length) {
            return @(NO);
        }
    }
    return @(YES);
}

#pragma mark - table view
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (!_selectCell) {
        return 0;
    }
    if (self.selectCell.type != EOEvaluateTypeGood) {
        return 2;
    }
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.selectCell.type != EOEvaluateTypeGood) {
        if (section == 1) {
//            if (self.tagCell.selectedMessage.length) {
//                return 1;
//            }
            return 2;
        }
    }else {
        if (section == 1) {
            return _starArray.count;
        }else if (section == 2) {
            return  1;
        }
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectCell.type != EOEvaluateTypeGood) {
        if (indexPath.section == 1) {
            if (indexPath.row == 0) {
                return _tagCell;
            }else {
                return _moreCell;
            }
        }
    }else {
        if (indexPath.section == 1) {
            return _starArray[indexPath.row];
        }else if (indexPath.section == 2) {
            return _inputCell;
        }
    }
    return _selectCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectCell.type != EOEvaluateTypeGood) {
        if (indexPath.section == 1) {
            if (indexPath.row == 0) {
                return [OMOrderEvaluateTagTableCell cellHeight];
            }else if(indexPath.row == 1) {
                return [OMOrderEvaluateSelectTableCell cellHeight];
            }
        }
    }else {
        if (indexPath.section == 1) {
            return [OMOrderEvaluateStarTableCell cellHeight];
        }else if (indexPath.section == 2) {
            return SM_SIZE(200);
        }
    }
    return [OMOrderEvaluateSelectTableCell cellHeight];
}

- (void)submitAction:(UIButton *)sender
{
    [OMHUD HUDShieldLoading:nil];
    NSString *content = nil;
    if (self.selectCell.type == EOEvaluateTypeGood) {
        content = self.inputCell.textView.text?:@"";
    }else {
        content = [self.tagCell.selectedMessage?:@"" stringByAppendingString:[NSString stringWithFormat:@"%@%@",self.tagCell.selectedMessage.length?@",":@"",self.moreCell.textView.text?:@""]];
    }
    [KTHttpUtils POST:@"member/review/save.jhtml"
           parameters:@{
                        @"review.score":[@(self.selectCell.type)stringValue],
                        @"review.shop_score":self.selectCell.type == EOEvaluateTypeGood?[@(self.shopCell.starView.point) stringValue]:@"0",
                        @"review.delivery_score":self.selectCell.type == EOEvaluateTypeGood?[@(self.shipCell.starView.point) stringValue]:@"0",
                        @"review.people_score":self.selectCell.type == EOEvaluateTypeGood?[@(self.menCell.starView.point) stringValue]:@"0",
                        @"review.content":content,
                        @"sn":self.order.sn,
                        }
             progress:nil
           completion:^(id responseObject, BOOL isSuccess, NSString *message) {
               if (isSuccess) {
                   [OMHUD HUDSuccess:@"评价成功!"];
                   [self backAction];
               }else  {
                   [OMHUD HUDError:message];
               }
    }];
}
@end
